#include <gtk/gtk.h>
#include <stdlib.h>
#include "callbacks.h"
#include "config.h"
#include "nls.h"
#include "views.h"
#include "window.h"
#include "ttorrent.h"

GtkStatusIcon * create_status_icon()
{
    gchar * path = g_build_filename(PIXMAP_DIR, "ttorrent.png", NULL);
    GdkPixbuf * pixbuf = gdk_pixbuf_new_from_file(path, NULL);
    g_free(path);
    gtk_window_set_default_icon(pixbuf);
    GtkStatusIcon * icon;
    icon = gtk_status_icon_new_from_pixbuf(pixbuf);
    gtk_status_icon_set_visible(icon, FALSE);
    gtk_status_icon_set_tooltip(icon, "Tom's torrent");
    g_object_ref(icon);
    return icon;
}

GtkWidget *
create_notebook(TTorrent * ttorrent, GtkWidget * toolbar)
{
    GtkWidget * widget;
    GtkWidget * child;
    GtkWidget * box;

    widget = gtk_notebook_new();

    child = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (child),
        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    ttorrent->info_view = gtk_alignment_new(0.0, 0.0, 0.0, 0.0);
    gtk_container_add(GTK_CONTAINER(ttorrent->info_view), create_info_view());
    gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (child), GTK_WIDGET(ttorrent->info_view));
    gtk_notebook_append_page(GTK_NOTEBOOK(widget), GTK_WIDGET(child), GTK_WIDGET(gtk_label_new(_("Info"))));

    child = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (child),
        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    ttorrent->peers_view = create_peers_view();
    gtk_container_add (GTK_CONTAINER (child), GTK_WIDGET(ttorrent->peers_view));
    gtk_notebook_append_page(GTK_NOTEBOOK(widget), GTK_WIDGET(child), GTK_WIDGET(gtk_label_new(_("Connections"))));

    child = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (child),
        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    ttorrent->blocks_view = create_blocks_view();
    gtk_container_add (GTK_CONTAINER (child), GTK_WIDGET(ttorrent->blocks_view));
    gtk_notebook_append_page(GTK_NOTEBOOK(widget), GTK_WIDGET(child), GTK_WIDGET(gtk_label_new(_("Pieces"))));

    child = gtk_scrolled_window_new(NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (child),
        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    ttorrent->files_view = create_files_view();
    gtk_container_add (GTK_CONTAINER (child), GTK_WIDGET(ttorrent->files_view));

    box = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(box), child, TRUE, TRUE, 0);
    gtk_box_pack_start(GTK_BOX(box), toolbar, FALSE, FALSE, 0);

    gtk_notebook_append_page(GTK_NOTEBOOK(widget), GTK_WIDGET(box), GTK_WIDGET(gtk_label_new(_("Files"))));
    return widget;

}

void
create_window(TTorrent* ttorrent)
{

GtkActionEntry entries[] =
{
    {"FileMenu", NULL, N_("_File"), NULL, NULL, NULL},
    {"TorrentMenu", NULL, N_("_Torrent"), NULL, NULL, NULL},
    {"HelpMenu", NULL, N_("_Help"), NULL, NULL, NULL},
    {
        "Add", GTK_STOCK_ADD, N_("_Add"), "<control>O", N_("Add torrent"),
        G_CALLBACK (add_action_callback)
    },
    {
        "Remove", GTK_STOCK_REMOVE, N_("_Remove"), "<control>W", N_("Remove torrent"),
        G_CALLBACK (remove_action_callback)
    },
    {
        "Options", GTK_STOCK_PREFERENCES, N_("Preferences"), "<control>Q", N_("Preferences"),
        G_CALLBACK (options_action_callback)
    },
    {
        "PauseResume", GTK_STOCK_REFRESH, N_("Pause/Resume"), NULL, N_("Pause/Resume torrent"),
        G_CALLBACK (pause_resume_action_callback)
    },
    {
        "MoveUp", GTK_STOCK_GO_UP, N_("_Up"), NULL, N_("Move up"),
        G_CALLBACK (move_up_action_callback)
    },
    {
        "MoveDown", GTK_STOCK_GO_DOWN, N_("_Down"), NULL, N_("Move down"),
        G_CALLBACK (move_down_action_callback)
    },
    {
        "Exit", GTK_STOCK_QUIT, N_("E_xit"), "<control>Q", N_("Exit the program"),
        G_CALLBACK (exit_action_callback)
    },
    {
        "About", GTK_STOCK_ABOUT, N_("About"), NULL, N_("About the program"),
        G_CALLBACK (about_action_callback)
    },
    {
        "CommitPri", GTK_STOCK_APPLY, N_("Apply"), NULL, N_("Set priorities"),
        G_CALLBACK (commit_pri_action_callback)
    },
    {
        "RollbackPri", GTK_STOCK_INDEX, N_("Default"), NULL, N_("Set default priorities"),
        G_CALLBACK (rollback_pri_action_callback)
    }
};

    GtkWidget *vbox; //vbox with widgets
    GtkWidget *child; // menu, toolbar, vpaned, statusbar
    GtkWidget *widget; //grandchild

    GtkUIManager *uiManager;
    GtkAccelGroup *accelGroup;
    GtkActionGroup *actionGroup;
    GError *error;

    ttorrent->window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

    /* give the window a window border */

    /* give it the title */
    gtk_window_set_title (GTK_WINDOW (ttorrent->window), PACKAGE " " VERSION);

    /* open it a bit wider so that both the label and title show up */
    gtk_window_set_default_size (GTK_WINDOW (ttorrent->window), 600, 450);

    /* load the icon for the window; here we just load one, default icon */
    //iconfile = g_build_filename(PIXMAP_DIR, "ttorrent.png", NULL);
    //gtk_window_set_default_icon_from_file (iconfile, NULL);
    //g_free(iconfile);

    ttorrent->icon = create_status_icon();

    g_signal_connect (G_OBJECT(ttorrent->icon), "activate",
        G_CALLBACK (icon_activate), ttorrent);
    g_signal_connect (G_OBJECT(ttorrent->icon), "popup-menu",
        G_CALLBACK (icon_popup_menu), ttorrent);

    /* Connect the destroy event of the window with our on_destroy function
     * When the window is about to be destroyed we get a notificaiton and
     * stop the main GTK loop
     */

    g_signal_connect( G_OBJECT(ttorrent->window), "delete-event",
        G_CALLBACK (delete_event_handler), ttorrent->icon);

    g_signal_connect (G_OBJECT(ttorrent->window), "destroy",
        G_CALLBACK (destroy_handler), NULL);

    /* create main box */
    vbox = gtk_vbox_new (FALSE, 0);

    actionGroup = gtk_action_group_new ("TTActions");
    gtk_action_group_set_translation_domain(actionGroup, GETTEXT_PACKAGE);
    gtk_action_group_add_actions (actionGroup, entries,
      G_N_ELEMENTS (entries), ttorrent);

    uiManager = gtk_ui_manager_new ();
    gtk_ui_manager_insert_action_group (uiManager, actionGroup, 0);

    accelGroup = gtk_ui_manager_get_accel_group (uiManager);
    gtk_window_add_accel_group (GTK_WINDOW (ttorrent->window), accelGroup);

    if (!gtk_ui_manager_add_ui_from_file
        (uiManager, PACKAGE_DIR "/ttorrent.xml", &error))
    {
        g_message (_("Building menus failed: %s"), error->message);
        g_error_free (error);
        exit(-1);
    }

    /* create and add menubar */
    child = gtk_ui_manager_get_widget (uiManager, "/Menubar");
    gtk_box_pack_start (GTK_BOX (vbox), child, FALSE, FALSE, 0);

    /* create and add toolbar */
    child = gtk_ui_manager_get_widget (uiManager, "/Toolbar");
    gtk_box_pack_start (GTK_BOX (vbox), child, FALSE, FALSE, 0);

    /* create main paned=2*windows */
    child = gtk_vpaned_new();

    /*first window with list of torrents*/
    widget = create_torrents_view (ttorrent);

    gtk_paned_pack1(GTK_PANED(child), widget, TRUE, TRUE);
    /* connect context menu signal handlers */
    g_signal_connect(G_OBJECT(ttorrent->torrents_view), "button-press-event", (GCallback) torrents_view_on_button_pressed, ttorrent);
    g_signal_connect(G_OBJECT(ttorrent->torrents_view), "popup-menu", (GCallback) torrents_view_on_popup_menu, ttorrent);

    widget = create_notebook(ttorrent, gtk_ui_manager_get_widget (uiManager, "/FilesToolbar"));
    /*second window with notebook*/
    gtk_paned_pack2(GTK_PANED(child), widget, TRUE, TRUE);

    gtk_box_pack_start (GTK_BOX (vbox), child, TRUE, TRUE, 0);

    /* create an pack statusbar */
    gtk_box_pack_start (GTK_BOX (vbox), create_statusbar(ttorrent), FALSE, FALSE, 0);

    /* Put the box into the main window. */
    gtk_container_add (GTK_CONTAINER (ttorrent->window), vbox);

    /* make sure that everything, window and childs, are visible */
    gtk_widget_show_all (ttorrent->window);
 }
